package com.zhu.controller.admin;

import com.zhu.base.http.RestResponse;
import com.zhu.domain.*;
import com.zhu.domain.enums.OperationEnum;
import com.zhu.domain.enums.PropTypeEnum;
import com.zhu.model.admin.GetAllInventoryItemVM;
import com.zhu.model.admin.TableColumnVM;
import com.zhu.model.inventory.InventoryItemVM;
import com.zhu.model.inventoryRecord.InventoryRecordVM;
import com.zhu.model.inventory.InventoryVM;
import com.zhu.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static java.util.UUID.randomUUID;

/**
 * @author JoyBoyZH
 * @description 库存管理
 * @date 2022/6/3
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/gim/api/admin/manageInventory")
@Api(tags = "管理员库存模块说明")
public class AdminInventoryController {
    private final ModelMapper modelMapper;
    private final WarehouseService warehouseService;
    private final InventoryService inventoryService;
    private final InventoryRecordService inventoryRecordService;
    private final InventoryRecordItemService inventoryRecordItemService;

    private final TableColumnService tableColumnService;

    @Autowired
    public AdminInventoryController(ModelMapper modelMapper, WarehouseService warehouseService, InventoryService inventoryService, InventoryRecordService inventoryRecordService, InventoryRecordItemService inventoryRecordItemService, TableColumnService tableColumnService) {
        this.modelMapper = modelMapper;
        this.warehouseService = warehouseService;
        this.inventoryService = inventoryService;
        this.inventoryRecordService = inventoryRecordService;
        this.inventoryRecordItemService = inventoryRecordItemService;
        this.tableColumnService = tableColumnService;
    }

    @PostMapping(value = "/selectInventory/{warehouseId}")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("查询库存说明")
    public RestResponse<Object> selectInventory(@PathVariable Integer warehouseId) {
        List<InventoryItemVM> list = inventoryService.getByRelevance(warehouseId);
        if(list.isEmpty()){
            return RestResponse.failure("该仓库库存为0");
        }
        GetAllInventoryItemVM model = new GetAllInventoryItemVM();
        List<TableColumn> tableColumnList = tableColumnService.getList(PropTypeEnum.INVENTORYITEM.getCode());
        List<TableColumnVM> tableColumnVMList = new ArrayList<>();
        for (TableColumn tc : tableColumnList) {
            TableColumnVM tableColumnVM = new TableColumnVM();
            tableColumnVM.setProp(tc.getProp());
            tableColumnVM.setPropName(tc.getPropName());
            tableColumnVMList.add(tableColumnVM);
        }
        model.setInventoryItemVM(list);
        model.setTableColumnList(tableColumnVMList);
        model.setTotalNum(list.size());
        return RestResponse.ok(model);

    }


    @PostMapping(value = "/inputInventory")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("入库接口说明")
    public RestResponse<Object> inputInventory(@RequestBody InventoryRecordVM model) {
        Warehouse warehouse = warehouseService.getByName(model.getWarehouseName());
        int totalInputInventory = 0;
        if(model.getInventoryVMList().isEmpty()){
            return RestResponse.failure("无相关货品！");
        }
        for (InventoryVM inventoryVM : model.getInventoryVMList()) {
            totalInputInventory += inventoryVM.getNumber();
        }
        if (totalInputInventory > warehouse.getCapacity() - warehouse.getTotalInventory()) {
            return RestResponse.failure("仓库容量不足！");
        } else {
            warehouse.setTotalInventory(warehouse.getTotalInventory() + totalInputInventory);
            warehouseService.updateWarehouse(warehouse);
        }

        InventoryRecord insertInventory = modelMapper.map(model, InventoryRecord.class);
        insertInventory.setTime(model.getTime());
        insertInventory.setOperation(OperationEnum.INPUT);
        insertInventory.setRid("record" + randomUUID().toString().substring(0,8));
        inventoryRecordService.saveInventoryRecord(insertInventory);
        int recordId = insertInventory.getRecordId();
        int warehouseId = warehouse.getWarehouseId();
        List<InventoryRecordItem> inventoryRecordItemList = new ArrayList<>();
        List<Inventory> inventoryList = new ArrayList<>();
        for (InventoryVM inventoryVM : model.getInventoryVMList()) {
            InventoryRecordItem inventoryRecordItem = new InventoryRecordItem();
            inventoryRecordItem.setRecordId(recordId);
            inventoryRecordItem.setGoodsId(inventoryVM.getGoodsId());
            inventoryRecordItem.setNumber(inventoryVM.getNumber());
            inventoryRecordItemList.add(inventoryRecordItem);
            Inventory inventory = inventoryService.getInventoryByCondition(warehouseId, inventoryVM.getGoodsId());
            if (inventory != null) {
                inventory.setNumber(inventory.getNumber() + inventoryVM.getNumber());
                inventoryService.updateInventory(inventory);
            } else {
                Inventory inventoryInfo = new Inventory();
                inventoryInfo.setWarehouseId(warehouseId);
                inventoryInfo.setGoodsId(inventoryVM.getGoodsId());
                inventoryInfo.setNumber(inventoryVM.getNumber());
                inventoryList.add(inventoryInfo);
            }
        }
        inventoryService.saveList(inventoryList);
        inventoryRecordItemService.saveList(inventoryRecordItemList);

        return RestResponse.ok("入库成功!");
    }

    @PostMapping(value = "/outInventory")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("出库接口说明")
    public RestResponse<Object> outInventory(@RequestBody InventoryRecordVM model) {
        Warehouse warehouse = warehouseService.getByName(model.getWarehouseName());

        InventoryRecord outInventory = modelMapper.map(model, InventoryRecord.class);
        outInventory.setOperation(OperationEnum.OUT);
        outInventory.setRid("record" + randomUUID().toString().substring(0,8));
        inventoryRecordService.saveInventoryRecord(outInventory);
        int recordId = outInventory.getRecordId();
        int warehouseId = warehouse.getWarehouseId();
        int totalOutInventory = 0;
        List<InventoryRecordItem> inventoryRecordItemList = new ArrayList<>();
        List<Inventory> inventoryList = new ArrayList<>();
        for (InventoryVM inventoryVM : model.getInventoryVMList()) {
            if(inventoryVM.getNumber() == 0){
                continue;
            }
            totalOutInventory += inventoryVM.getNumber();
            InventoryRecordItem inventoryRecordItem = new InventoryRecordItem();
            inventoryRecordItem.setRecordId(recordId);
            inventoryRecordItem.setGoodsId(inventoryVM.getGoodsId());
            inventoryRecordItem.setNumber(inventoryVM.getNumber());
            inventoryRecordItemList.add(inventoryRecordItem);
            Inventory inventory = inventoryService.getInventoryByCondition(warehouseId, inventoryVM.getGoodsId());
            int num = inventory.getNumber() - inventoryVM.getNumber();
            if (num != 0) {
                inventory.setNumber(num);
                inventoryService.updateInventory(inventory);
            } else {
                inventoryService.deleteById(inventory.getInventoryId());
            }
        }
        inventoryService.saveList(inventoryList);
        inventoryRecordItemService.saveList(inventoryRecordItemList);
        warehouse.setTotalInventory(warehouse.getTotalInventory() - totalOutInventory);
        warehouseService.updateWarehouse(warehouse);
        return RestResponse.ok("出库成功!");
    }



}
